//
//  API.swift
//  qimiao
//
//  Created by wjk on 2018/9/28.
//  Copyright © 2018 wjk. All rights reserved.
//

import Foundation
import Moya

enum API {
    case login(username: String,password:String,captcha:String)
    case cap()
    case code(user_phone:String,type:String,captcha:String)
    case codeesp(mobile:String,usage:String)
    case checkCode(user_phone:String, type:String, cap:String)
    case phonemodify(p1:String,p2:String,code:String)
    case register(param:[String:String])
    case forgotPwd(param:[String:String])
    case enterpriseinfo()
    case accountdetail()
    case bulletin_id(id:String)
    case message_id(id:String)
    case logout()
    case bulletin(param:[String:Any])
    case banner()
    case inoutlists(param:[String:Any])
    case myoffer(alias:String,status:String,variety_id:String,page:String)  //我的报价
    case myofferee(alias:String,status:String,variety_id:String,page:String)  //我的被报价
    case fair(type: Int?,direction: Int?,categoryId: Int?,beginAt: String?,endAt:String?,page: Int?, trade_type:String)
    case fairPlay()//观摩模式
    case receiving(id:String)//应价
    case bidingCancel(id:String)
    case bidingMessage(id:String)
    case replyMessage(id:String,content:String)
    case bankaccountpost(param:[String:Any])
    case bankaccget()
    case daibiaopost(param:[String:Any])
    case daibiaoget()
    case userinfopost(param:[String:Any])
    case userinfoget()
    case jingjiasession(id:String)
    case guapaisession(id:String)
    case xieshangsession(id:String)
    case jingjiaing(id:String, sid:String)
    case jingjiabid(id:String,sid:String)
    case jingjiawin(id:String)
    case jingjiatab(id:String)
    case jingjiast(fid:String,sid:String,tid:String,bid_price:String,last_price:String)
    case jingjias(id:String,sid:String,t:String,bid_price:String,last_price:String)
    case fairtargets(fair_id:String,target_id:String)
    case guapairuntime(id:String)
    case guapaiwin(id:String)
    case guapais(id:String,t:String,num:String)
    case xieshangbiding(fid:String,tid:String,price:String,content:String,delivery_point:String)
    case xieshangruntime(id:String)
    case xieshangwin(id:String)
    case xieshangs(id:String,t:String,price:String,content:String,delivery_point:String)
    case guapaibiding(fid:String,tid:String,num:String)
    case fileToken()
    case uploadImage(url:String,token:String,data:Data)
    case bank()
    case bankReal(id:String)
    case city1()
    case city2(id:String)
    case city3(id:String)
    case fairtarget(id:String,page:String,alig:String?,type:String?)
    case guapaideposit(fid:String,tid:String,num:String)
    case unread()
    case transactions(operation_time_begin:String?, operation_time_end:String?,direction:Int?,business_type:Int?,page:Int?)
    case bType()
    case version(v:String)
    case relationMark(enterprise_id:String)
    case relationMarkPost(enterprise_id:String, relation_remark:String)
}

extension API : TargetType {
    var baseURL: URL {
        switch self {
        case .uploadImage(let url,_,_):
            return URL.init(string: url)!
        default:
            return URL.init(string: AppConstants.apiEndpoint)!
        }
//        path
    }
    
    var path: String {
        var string = ""
        switch self {
        case .cap():
            return  "/api/v1/captcha" //新增新的使用方法的接口范例            
        case .login(_,_,_):
            string = "/api/v1/oauth/token"
            break
        case .bType(): string = "/api/v1/fund/business-type"
            break
        case .code(_, _, _):
            string =  "/api/v1/phone-captcha"
            break
        case .phonemodify(_, _, _):return "/api/v1/user/phone-modify"
        case .register(_):
            string = "/api/v1/register"
            break
        case .checkCode(_, _ , _): return "/api/v1/user/phone-captcha-check"
        case .codeesp(_, _): return "/api/v1/esp/authcode/send"
        case .transactions(_,_,_,_,_):
            string = "/api/v1/fund/record-user"
            break
        case .forgotPwd(_):
            string = "/api/v1/forgot-password"
            break
        case .enterpriseinfo():string = "/api/v1/user/enterprise-info"
            break
        case .accountdetail():string = "/api/v1/fund/account-detail"
            break
        case .logout():string =  "/api/v1/logout"
            break
        case .banner():string = "/api/v1/banner"
            break
        case .bulletin(_):string = "/api/v1/bulletin"
            break
        case .fair: return "/api/v1/fair"//获取专场
        case .fairPlay: return "/api/v1/playhouse"//获取专场
        case .inoutlists(_):string = "/api/v1/fund/inout-lists"
            break
        case .myoffer(_,_,_,_):string = "/api/v1/xieshang/trade/my-offer"
            break
        case .myofferee(_,_,_,_):string = "/api/v1/xieshang/trade/my-offeree"
            break
        case .receiving(let id): string = "/api/v1/xieshang/receiving/\(id)"
            break
        case .bidingCancel(let id): string = "/api/v1/xieshang/biding/\(id)/cancel"
            break
        case .bidingMessage(let id): string = "/api/v1/xieshang/biding/\(id)/message"
            break
        case .replyMessage(let id, _): string = "/api/v1/xieshang/biding/\(id)/reply-message"
            break
        case .bankaccountpost(_): string = "/api/v1/user/bank-account"
            break
        case .bankaccget():string = "/api/v1/user/bank-account"
            break
        case .daibiaoget():string = "/api/v1/user"
            break
        case .daibiaopost(_):string = "/api/v1/user"
            break
        case .userinfopost(_):string = "/api/v1/user/info"
            break
        case .userinfoget():string = "/api/v1/user/info"
            break
        case .fileToken():string = "/api/v1/upload/token"
            break
        case .uploadImage(_,_,_):string = ""
            break
        case .relationMark(let enterprise_id): string = "/api/v1/user/relation/\(enterprise_id)"
            break
        case .relationMarkPost(let enterprise_id, _):string = "/api/v1/user/relation/\(enterprise_id)"
            break
        case .version(_):string = "/api/v1/app/version"
            break
        case .unread():string = "/api/v1/message/unread-count"
            break
        case .bulletin_id(let id ):string = "/api/v1/bulletin/\(id)"
            break
        case .message_id(let id):string = "/api/v1/message/\(id)"
            break
        case .guapaibiding(let fid, let tid, _):string = "/api/v1/guapai/biding/\(fid)/target/\(tid)"
            break
        case .jingjiast(let fid, let sid, let tid, _, _):string = "/api/v1/jingjia/\(fid)/s/\(sid)/t/\(tid)"
            break
        case .xieshangbiding(let fid, let tid, _, _, _):string = "/api/v1/xieshang/biding/\(fid)/target/\(tid)"
            break
        case .fairtargets(let fair_id, let target_id):string = "/api/v1/fair/\(fair_id)/target/\(target_id)"
            break
        case .jingjiasession(let id):
            if (UIApplication.shared.delegate as! AppDelegate).isplayData.count > 0 {
                string = "/api/v1/biding/\(id)/runtime"
            }else {
                string = "/api/v1/jingjia/\(id)/runtime"
            }
            break
        case .jingjiaing(let id,_):string = "/api/v1/jingjia/\(id)/target-by-section"
            break
        case .jingjiabid(let id,_):string = "/api/v1/jingjia/\(id)/my-bid-target"
            break
        case .jingjiawin(let id):string = "/api/v1/jingjia/\(id)/my-win-target"
            break
        case .jingjiatab(let id):string = "/api/v1/jingjia/\(id)/target-tab-list"
            break
        case .jingjias(let id,let sid, let t, _, _):string = "/api/v1/jingjia/\(id)/s/\(sid)/t/\(t)"
            break
        case .guapais(let id, let t, _):string = "/api/v1/guapai/biding/\(id)/target/\(t)"
            break
        case .xieshangs(let id, let t, _, _, _):string = "/api/v1/xieshang/biding/\(id)/target/\(t)"
            break
        case .guapaisession(let id):string = "/api/v1/guapai/\(id)/runtime"
            break
        case .guapairuntime(let id):string = "/api/v1/guapai/\(id)/target/runtime"
            break
        case .guapaiwin(let id):string = "/api/v1/guapai/\(id)/my-win-target"
            break
        case .xieshangruntime(let id):string = "/api/v1/xieshang/\(id)/target/runtime"
            break
        case .xieshangwin(let id):string = "/api/v1/xieshang/\(id)/my-win-target"
            break
        case .xieshangsession(let id):string = "//api/v1/xieshang/\(id)/runtime"
            break
        case .bank():string = "/api/v1/banks/banks.json"
            break
        case .bankReal(let id):string = "/api/v1/banks/bank-completely/\(id).json"
            break
        case .city1():string = "/api/v1/region/provinces.json"
            break
        case .city2(let id):string = "/api/v1/region/provinces/\(id).json"
            break
        case .city3(let id):string = "/api/v1/region/cities/\(id).json"
            break
        case .fairtarget(let id,_,_,_):string = "/api/v1/fair/\(id)/target"
            break
        case .guapaideposit(let fid, let tid, _):string = "/api/v1/guapai/\(fid)/target/\(tid)/deposit"
            break
        }
        if (UIApplication.shared.delegate as! AppDelegate).isplayData.count > 0 {
            string = string.replacingOccurrences(of: "api/v1", with: "api/v1/playhouse")
            string = string.replacingOccurrences(of: "jingjia", with: "fair")
        }
           return string
    }
    
    var method: Moya.Method {
        
        switch self {
        case .login(_,_,_): return .post
        case .code(_,_,_): return .post
        case .register(_): return.post
        case .forgotPwd(_): return .post
        case .logout(): return .post
        case .phonemodify(_,_,_):return .post
        case .receiving(_): return .post
        case .bidingCancel(_): return .post
        case .bidingMessage(_): return .get
        case .replyMessage(_, _): return .post
        case .bankaccountpost(_): return .post
        case .userinfopost(_):return .post
        case .checkCode(_, _, _):return .post
        case .codeesp(_, _):return .post
        
        case .daibiaopost(_):return .post
        case .uploadImage(_,_,_): return .post
        case .xieshangbiding:return .post
        case .guapaibiding:return .post
        case .jingjiast:return .post
        case .relationMarkPost(_, _):return .post
        default: return .get
        }
        
    }
    
    var sampleData: Data {
        return "暂无数据".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        switch self {
        case .login(let username, let password, let captcha):
                return Task.requestParameters(parameters: ["app_id":AppConstants.app_id,"app_secret":AppConstants.app_secret,"username":username,"password":password,"captcha":captcha], encoding: URLEncoding.default)
        case .cap():
            return Task.requestPlain
        case .code(let user_phone, let type, let captcha):
            return Task.requestParameters(parameters: ["user_phone":user_phone,"type":type,"captcha":captcha], encoding: URLEncoding.default)
        case .inoutlists(let param):
            return Task.requestParameters(parameters: param, encoding: URLEncoding.default)
        case .register(let param):
            return Task.requestParameters(parameters: param , encoding: URLEncoding.default)
        case .forgotPwd(let param):
            return Task.requestParameters(parameters: param , encoding: URLEncoding.default)        
        case .uploadImage(_,let token,let data):return .uploadCompositeMultipart([MultipartFormData(provider: .data(data), name: "file",fileName: "\(NSUUID.init().uuidString.replacingOccurrences(of: "-", with: "")).jpg", mimeType: "image/jpeg") ], urlParameters: ["key":"file","token":token])
        case .fileToken():return Task.requestParameters(parameters: ["size":"12","type":"image"], encoding: URLEncoding.default)
        case .phonemodify(let p1, let p2, let code):return Task.requestParameters(parameters: ["old_phone":p1,"new_phone":p2,"new_phone_code":code], encoding: URLEncoding.default)
        case .guapaideposit(_, _, let num):return Task.requestParameters(parameters: ["num":num], encoding: URLEncoding.default)
        case .relationMarkPost(_, let relation_remark):return Task.requestParameters(parameters: ["relation_remark":relation_remark], encoding: URLEncoding.default)
        case .jingjiast(_, _, _, let bid_price, let last_price):return Task.requestParameters(parameters: ["bid_price":bid_price,"last_price":last_price], encoding: URLEncoding.default)
        case .banner():return Task.requestParameters(parameters: ["p":"24,25"], encoding: URLEncoding.default)
        case .checkCode(let p,let t,let c):return Task.requestParameters(parameters: ["user_phone":p,"captcha":c,"type":t], encoding: URLEncoding.default)
        case .fairtarget(_, let page,let align, let type): return Task.requestParameters(parameters: ["page":page,"pre_count":AppConstants.pre_count,"alias":align ?? "", "variety_id": type ?? ""], encoding: URLEncoding.default)
        case .jingjiaing(_,let sid):return Task.requestParameters(parameters: ["section_id":sid], encoding: URLEncoding.default)
        case .codeesp(let mobile, let usage):return Task.requestParameters(parameters: ["mobile":mobile,"usage":usage], encoding: URLEncoding.default)
        case .jingjiabid(_, let sid): return Task.requestParameters(parameters: ["section_id":sid], encoding: URLEncoding.default)
        case .myofferee(let alias, let status, let variety_id,let page):return Task.requestParameters(parameters: ["alias":alias,"status":status,"variety_id":variety_id,"page":page], encoding: URLEncoding.default)
        case .myoffer(let alias, let status, let variety_id,let page):return Task.requestParameters(parameters: ["alias":alias,"status":status,"variety_id":variety_id,"page":page], encoding: URLEncoding.default)
        case .version(let v):return Task.requestParameters(parameters: ["p_type":"1","version_num":v], encoding: URLEncoding.default)
        case .daibiaopost(let param): return Task.requestParameters(parameters: param, encoding: URLEncoding.default)
        case .userinfoget():return Task.requestPlain
        case .jingjias(_, _, _, let bid_price, let last_price):return Task.requestParameters(parameters: ["bid_price":bid_price,"last_price":last_price], encoding: URLEncoding.default)
        case .xieshangs(_, _, let price, let content, let delivery_point):return Task.requestParameters(parameters: ["price":price,"content":content,"delivery_point":delivery_point], encoding: URLEncoding.default)
        case .userinfopost(let param):return Task.requestParameters(parameters: param, encoding: URLEncoding.default)
        case .replyMessage(_, let content): return Task.requestParameters(parameters: ["content":content], encoding: URLEncoding.default)
        case .guapaibiding(_, _, let num):return Task.requestParameters(parameters: ["num":num], encoding: URLEncoding.default)
        case .bankaccountpost(let param): return Task.requestParameters(parameters: param, encoding: URLEncoding.default)
        case .xieshangbiding(_, _, let price, let content, let delivery_point):return Task.requestParameters(parameters: ["price":price,"content":content,"delivery_point":delivery_point], encoding: URLEncoding.default)
        case .bulletin(let param):return Task.requestParameters(parameters: param, encoding: URLEncoding.default)            
        case .fair(let type, let direction, let categoryId, let beginAt, let endAt, let page, let trade_type):
            var para = [String:Any]()
            if let type = type{
                para["trade_type"] = type
            }
            if let dir = direction{
                para["direction"] = dir
            }
            if let cid = categoryId {
                para["category_id"] = cid
            }
            if let begin = beginAt{
                para["begin_at"] = begin
            }
            if let end = endAt{
                para["end_at"] = end
            }
            if let page = page{
                para["page"] = page
            }
            para["trade_type"] = trade_type
            return Task.requestParameters(parameters: para, encoding: URLEncoding.default)
        case .transactions(let operation_time_begin, let operation_time_end, let direction, let business_type, let page):
            var para = [String:Any]()
            para["page"] = page
            if let start = operation_time_begin {
                para["operation_time_begin"] = start
            }
            if let end = operation_time_end{
                para["operation_time_end"] = end
            }
            if let dire = direction {
                para["direction"] = dire
            }
            if let type = business_type{
                para["business_type"] = type
            }
            
            return Task.requestParameters(parameters: para, encoding: URLEncoding.default)
            
            default:return Task.requestPlain
        }
        
    }
    
    var headers: [String : String]? {
        return NetWorkManager.shared.headers()
    }
}
